<?php

/**
 * 浏览
 * Class view
 */
class view {

    private $db_content;
    private $db_view;

    public function  __construct() {
        $this->db_content = pc_base::load_model('content_model');
        $this->db_view = pc_base::load_model('view_model');
    }

    //浏览计数
    public function add() {
        $uid = (int)param::get_cookie('_userid');
        $catid = $_POST['catid'] ? (int)$_POST['catid'] : 0;
        $id = $_POST['id'] ? (int)$_POST['id'] : 0;
        $ymd = date('Y-m-d');
        $ymdhis = date('Y-m-d H:i:s');

        //记录
        $sql = "insert into v9_view (uid,a_catid,a_id,ymd,ymdhis,num) values ($uid,$catid,$id,'$ymd','$ymdhis',1) on duplicate key update num=num+1";
        $this->db_view->query($sql);

        //更新数量
        $MODEL = getcache('model', 'commons');
        $CATEGORYS = getcache('category_content_1', 'commons');
        $modelid = $CATEGORYS[$catid]['modelid'];
        $table = $MODEL[$modelid]['tablename'];
        $this->db_content->table_name = 'v9_' . $table;
        $sql = "update {$this->db_content->table_name} set num_view=num_view+1 where id=$id";
        $this->db_content->query($sql);

        json_exit(1);
    }

}